package de.hft_stuttgart.dsa.ctt.model;

import java.util.List;

/**
 * This matrix is used within the Pre-Processing steps. Do not use this matrix
 * to assign courses to.
 * 
 * @author Matthias Huber
 */
public class CourseCompetitionMatrix extends AbstractMatrix {

	public CourseCompetitionMatrix(List<Room> rooms, List<Period> periods, int numberOfCourses) {
		super(rooms, periods);

		if (null != rooms && null != periods) {
			periodRoomMatrix = new Integer[periods.size()][rooms.size()];
			// initialized with the number of courses so that after calculation
			// the figures represent the number of assignable courses. Example:
			// one timeslot is initialized with numberOfCourses = 30 and
			// afterwards 18 courses cannot be assigned to this timeslot because
			// of UnavailablilityConstraints, so the number of this timeslot is
			// 12 which means that 12 courses can be assigned to this timeslot.
			initializeMatrix(numberOfCourses);
		} else {
			throw new RuntimeException("Rooms or Periods must not be null");
		}
	}

	private void initializeMatrix(int numberOfCourses) {
		for (int i = 0; i < periodRoomMatrix.length; i++) {
			for (int j = 0; j < periodRoomMatrix[i].length; j++) {
				periodRoomMatrix[i][j] = new Integer(numberOfCourses);
			}
		}
	}

	public void updateSingleTimeslot(int unavailabilityPeriod) {
//		System.out.println("Calculated period: " + unavailabilityPeriod);

		// just update all rooms within the timeslot
		for (int i = 0; i < periodRoomMatrix[unavailabilityPeriod].length; i++) {
			// System.out.println(i);
			periodRoomMatrix[unavailabilityPeriod][i] = ((Integer) periodRoomMatrix[unavailabilityPeriod][i]).intValue() - 1;
		}
	}

	public CourseCompetitionMatrix clone() throws CloneNotSupportedException {
		CourseCompetitionMatrix object = (CourseCompetitionMatrix) super.clone();
		object.periodRoomMatrix = new Object[periods.size()][rooms.size()];
		for (int i = 0; i < object.periodRoomMatrix.length; i++) {
			for (int j = 0; j < object.periodRoomMatrix[i].length; j++) {
				object.periodRoomMatrix[i][j] = new Integer((Integer) periodRoomMatrix[i][j]).intValue();
			}
		}
		return object;
	}
}
